package com.ruibang.glass.material.service.impl.bhbj;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.domain.req.ycl.ManageOutBoundReq;
import com.ruibang.glass.material.domain.req.ycl.ManageOutInReq;
import com.ruibang.glass.material.dto.OutAndInManagementDTO;
import com.ruibang.glass.material.entity.bgjj.MaterialBGJJManagementInventory;
import com.ruibang.glass.material.entity.bhbj.MaterialBHBJOutboundManagement;
import com.ruibang.glass.material.entity.ycl.MaterialOutboundManagement;
import com.ruibang.glass.material.mapper.bhbj.GMaterialBHBJOutboundManagementMapper;
import com.ruibang.glass.material.service.bhbj.GMaterialBHBJOutboundManagementService;
import com.ruibang.glass.material.service.impl.CommonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 出库管理表 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-01
 */
@Service
public class GMaterialBHBJOutboundManagementServiceImpl extends ServiceImpl<GMaterialBHBJOutboundManagementMapper, MaterialBHBJOutboundManagement> implements GMaterialBHBJOutboundManagementService {

    @Autowired
    private CommonService commonService;

    @Override
    public IPage<MaterialBHBJOutboundManagement> getPage(ManageOutBoundReq materialInventoryReq) {
        LambdaQueryWrapper<MaterialBHBJOutboundManagement> queryWrapper = Wrappers.<MaterialBHBJOutboundManagement>lambdaQuery();
        if (StringUtils.isNotBlank(materialInventoryReq.getOrderNumber())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getOrderNumber, materialInventoryReq.getOrderNumber());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getType())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getType, materialInventoryReq.getType());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getStatus())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getStatus, materialInventoryReq.getStatus());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getApproveStatus())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getApproveStatus, materialInventoryReq.getApproveStatus());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getPreparer())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getPreparer, materialInventoryReq.getPreparer());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getBatchNumber())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getBatchNumber, materialInventoryReq.getBatchNumber());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getItemName())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getItemName, materialInventoryReq.getItemName());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getBarcodeNumber())) {
            queryWrapper.like(MaterialBHBJOutboundManagement::getBarcodeNumber, materialInventoryReq.getBarcodeNumber());
        }
        if (materialInventoryReq.getFinishStartDate() != null) {
            queryWrapper.ge(MaterialBHBJOutboundManagement::getFinishDate, materialInventoryReq.getFinishStartDate());
        }
        if (materialInventoryReq.getFinishEndDate() != null) {
            queryWrapper.le(MaterialBHBJOutboundManagement::getFinishDate, materialInventoryReq.getFinishEndDate());
        }
        Page<MaterialBHBJOutboundManagement> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(materialInventoryReq.getPageNo(), materialInventoryReq.getPageSize()),
                queryWrapper.orderByDesc(MaterialBHBJOutboundManagement::getCreateTime)
        );
        return materialWarehouseInfoPage;
    }

    @Override
    public IPage<OutAndInManagementDTO> getPage(ManageOutInReq manageOutInReq) {
        return baseMapper.getPage(new Page<>(manageOutInReq.hashCode(), manageOutInReq.getPageSize()), manageOutInReq);
    }

    @Override
    public void addMaterialBHBJOutboundManagement(MaterialBHBJOutboundManagement materialOutboundManagement) {
        materialOutboundManagement.setSpStatus(Constants.ONE_STR);
        baseMapper.insert(materialOutboundManagement);
        BpmProcessInstanceCreate instanceCreate = new BpmProcessInstanceCreate();
        instanceCreate.setBusinessKey(materialOutboundManagement.getId());
        instanceCreate.setMenuId(materialOutboundManagement.getMenuId());
        //发起流程
        if (StringUtils.isNotBlank(materialOutboundManagement.getPreparerId())) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_STARTER_USER_ID, materialOutboundManagement.getPreparerId());
            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_BUSINESS_KEY, materialOutboundManagement.getId());
            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_MENU_ID, materialOutboundManagement.getMenuId());
            instanceCreate.setVariables(variables);
        }
        commonService.startProcess(instanceCreate);
    }


    @Override
    public MaterialBHBJOutboundManagement getOneList(String id) {
        MaterialBHBJOutboundManagement materialManagementInventory = baseMapper.selectById(id);
        List<MaterialBHBJOutboundManagement> materialManagementInventories = baseMapper.selectList(Wrappers.<MaterialBHBJOutboundManagement>lambdaQuery().eq(MaterialBHBJOutboundManagement::getOrderNumber, materialManagementInventory.getOrderNumber()));
        materialManagementInventory.setMaterialBHBJOutboundManagements(materialManagementInventories);
        return materialManagementInventory;
    }

    @Override
    public void updateInHouse(MaterialBGJJManagementInventory materialManagementInventory) {

    }
}
